/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Administrator
 */
public class ButtonImage extends Canvas {

    private String name;
    private Image image;
    private Image imagePressed;
    private Image imageSelected;
    //MainCanvas mainCanvas;
    private boolean isNothing = false;
    private boolean isPressed = false;
    private boolean isSelected = false;
    private boolean isDragged = false;
    //-
    public int xImage = 0;
    public int yImage = 0;
    public int imageHeight = 0;
    public int imageWidth = 0;
    //----- area
    int xAwal = 0;
    int yAwal = 0;
    int xAkhir = 0;
    int yAkhir = 0;
    //-------
    int xT;
    int yT;
//---if dragged
    boolean isMovable = false;
//----
    int anchor;
//----buat gesture
    //private GestureRecognizer recognizer;
    //private int gesture;
    //private boolean show;
    int x;
    int y;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
//----

    public boolean isIsNothing() {
        return isNothing;
    }

    public void setIsNothing(boolean isNothing) {
        this.isNothing = isNothing;
    }

    public boolean isIsPressed() {
        return isPressed;
    }

    public void setIsPressed(boolean isPressed) {
        this.isPressed = isPressed;
    }

    public boolean isIsSelected() {
        return isSelected;
    }

    public void setIsSelected(boolean isSelected) {
        this.isSelected = isSelected;
    }

    public ButtonImage(String name, Image img, Image imgPressed,
            Image imgSelected, int x, int y, int xyA[], int anchor) {

        //this.mainCanvas = mainCanvas;
        this.name = name;
        this.image = img;
        this.imagePressed = imgPressed;
        this.imageSelected = imgSelected;
        //scrollTab(lastPointerX - x, lastPointerY - y);
        this.xImage = x;
        this.yImage = y;
        this.xAwal = xyA[0];
        this.xAkhir = xyA[1];
        this.yAwal = xyA[2];
        this.yAkhir = xyA[3];
        this.anchor = anchor;

        imageHeight = image.getHeight();
        imageWidth = image.getWidth();

        isNothing = true;
        //isPressed = true;
    }

    public void drawStringBottom(Graphics g, int x, int y, String s) {
        if (s != null) {
            //g.drawString(s, mainCanvas.getWidth() / 2, mainCanvas.getHeight(), Graphics.HCENTER | Graphics.BOTTOM);
            g.drawString(s, x, y, Graphics.HCENTER | Graphics.BOTTOM);
        }
    }

    public void paint(Graphics g) {
        isMovable = false;
        //xImage = xImage-(imagePressed.getWidth()-image.getWidth())/2;
        //yImage = yImage-(imagePressed.getHeight()-imagePressed.getHeight())/2;
        if (isNothing) {
            if (image != null) {
                g.drawImage(image, xImage, yImage, anchor);
            }
        }
        if (isPressed) {
            if (imagePressed != null) {
                g.drawImage(imagePressed, xImage,
                        yImage, anchor);
            }
        }
        if (isSelected) {
            if (imageSelected != null) {
                g.drawImage(imageSelected, xImage, yImage, anchor);
            }
        }
    }

    public void draw(Graphics g, int translationX, int translationY) {

        isMovable = true;
        xT = translationX;
        yT = translationY;


        if (isNothing) {
            if (image != null) {
                g.drawImage(image, xImage - translationX,
                        yImage - translationY, anchor);
            }
        }
        if (isPressed) {
            if (imagePressed != null) {
                g.drawImage(imagePressed, xImage - translationX,
                        yImage - translationY, anchor);
            }
        }
        if (isSelected) {
            if (imageSelected != null) {
                g.drawImage(imageSelected, xImage - translationX,
                        yImage - translationY, anchor);
            }
        }
    }

    public void pointerPressed(int x, int y) {
        //recognizer = new GestureRecognizer(x, y);
        this.x = x;
        this.y = y;


        //show = false;
        //scrollTab(lastPointerX - x, lastPointerY - y);

        if (isMovable) {
            if (x > xImage - xT
                    && x < (xImage - xT + imageWidth)
                    && y > yImage - yT
                    && y < (yImage - yT + imageHeight)) {
                isNothing = false;
                isPressed = true;
                isSelected = false;
                isDragged = false;

                repaint();
            }
        } else {
            if (x > xImage
                    && x < (xImage + imageWidth)
                    && y > yImage
                    && y < (yImage + imageHeight)) {
                isNothing = false;
                isPressed = true;
                isSelected = false;
                isDragged = false;

                repaint();
            }
        }
    }

    public void pointerDragged(int x, int y) {
        //recognizer.addMotion(x, y);
        //scrollTab(lastPointerX - x, lastPointerY - y);
        this.x = x;
        this.y = y;

        if (x > xImage - xT
                && x < (xImage - xT + imageWidth)
                && y > yImage - xT
                && y < (yImage - yT + imageHeight)) {
            isDragged = true;
        } else {
            isDragged = false;
        }

    }

    public void pointerReleased(int x, int y) {
        //recognizer.endGesture(x, y);
        //gesture = recognizer.getGesture();
        this.x = x;
        this.y = y;
        //show = true;
        /*
        System.out.println(xAwal);
        System.out.println(xAkhir);
        System.out.println(yAwal);
        System.out.println(yAkhir);
        System.out.println("+200 : " + yAkhir + 200);
         * 
         */
        //scrollTab(lastPointerX - x, lastPointerY - y);

        if (isMovable) {
            if (x > xImage - xT
                    && x < (xImage - xT + imageWidth)
                    && y > yImage - yT
                    && y < (yImage - yT + imageHeight)) {
                //System.out.println("Button Released!");
                //System.out.println("dalam!");
                //System.out.println(getName());

                isNothing = false;
                isPressed = false;
                isSelected = true;
                isDragged = false;
            } else if (x > xAwal - xT
                    && x < xAkhir - xT
                    && y > yAwal - yT
                    && y < yAkhir - yT) {
                //System.out.println("luar!");
                isNothing = true;
                isPressed = false;
                isSelected = false;
                isDragged = false;
            }

            repaint();
        } else if (x > xImage 
                && x < (xImage + imageWidth)
                && y > yImage 
                && y < (yImage + imageHeight)) {
            //System.out.println("Button Released!");
            //System.out.println("dalam!");
            //System.out.println(getName());

            isNothing = false;
            isPressed = false;
            isSelected = true;
            isDragged = false;
        } else if (x > xAwal 
                && x < xAkhir
                && y > yAwal 
                && y < yAkhir) {
            //System.out.println("luar!");
            isNothing = true;
            isPressed = false;
            isSelected = false;
            isDragged = false;
        }
        repaint();

        /*else {5
        isNothing = true;
        isPressed = false;
        isSelected = false;
        repaint();
        isDragged = false;
        }
        }
         * 
         */
    }

    public void setPosition(int x, int y, int[] xyArea, int i) {
        //throw new UnsupportedOperationException("Not yet implemented");
        
        this.xImage = x;
        this.yImage = y;
        this.xAwal = xyArea[0];
        this.xAkhir = xyArea[1];
        this.yAwal = xyArea[2];
        this.yAkhir = xyArea[3];
        this.anchor = i;

        repaint();
    }
}
